package com.polaris.live.ui.user_space

import android.os.Bundle
import com.blankj.utilcode.util.ColorUtils
import com.gyf.immersionbar.ImmersionBar
import com.polaris.live.R
import com.polaris.live.adapter.GiftReceivedAdapter
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.network.parseState
import com.polaris.live.databinding.ActivityGiftListBinding
import com.polaris.live.translation.R.string
import com.polaris.live.utils.getLongOrString
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.polaris.live.viewmodel.UserSpaceViewModel
import com.polaris.live.widget.refresh.EmptyView
import com.polaris.live.widget.refresh.ErrorView

/**
 * 礼物列表
 *
 * @author Created by 长空 on 2023/11/01/004 09:42
 */
class GiftReceivedActivity :
    BaseVbActivity<UserSpaceViewModel, ActivityGiftListBinding>() {

    private val userId: Long by lazy { intent.getLongOrString(CommonConst.Gift.USER_ID, 0L) }

    private val emptyView by lazy { EmptyView(this) }

    private val errorView by lazy { ErrorView(this) }

    private val mAdapter: GiftReceivedAdapter by lazy {
        GiftReceivedAdapter()
    }

    override fun initImmersionBar() {
        ImmersionBar.with(this)
            .statusBarDarkFont(false)
            .titleBarMarginTop(mBinding.bvTitle)
            .transparentBar()
            .init()
    }

    override fun initView(savedInstanceState: Bundle?) {

        mBinding.recyclerView.adapter = mAdapter
        emptyView.setEmptyTip(R.drawable.ic_gift_empty, getString(string.no_receive_gift), ColorUtils.getColor(R.color.color_ffffff_50))

        initListener()
        initObserver()

        mViewModel.getGiftReceived(userId)
    }

    private fun initListener() {
        mAdapter.setOnItemClickListener { _, _, position ->
            if (UserManager.getUser().userId == userId) {
                Router.instance.setPath(RoutePath.GIFT_DETAIL_ACTIVITY)
                    .withLong(CommonConst.Gift.GIFT_ID, mAdapter.data[position].giftId)
                    .withString(
                        CommonConst.Gift.GIFT_PIC,
                        mAdapter.data[position].pic ?: ""
                    )
                    .withString(
                        CommonConst.Gift.GIFT_NAME,
                        mAdapter.data[position].giftName ?: ""
                    )
                    .navigation(this)
            }
        }
    }

    private fun initObserver() {
        mViewModel.giftListLiveData.observe(this) { resultState ->
            parseState(resultState, {
                if(it.isEmpty()){
                    mAdapter.setEmptyView(emptyView)
                }
                mAdapter.setList(it)
            }, {
                errorView.setText(it.errorMsg)
                errorView.setTextColor(ColorUtils.getColor(R.color.color_ffffff_50))
                mAdapter.setEmptyView(errorView)
            })
        }
    }

}